package org.example.Polls;

import java.util.HashMap;
import java.util.Map;

import android.util.Log;

/**
 * Simple singleton class that supports two static methods;
 * One to get() a poll from the cache, and another to add() a
 * poll to the cache.
 * @author Brent Smith 
 */
public class PollCache {
	
	private static final String TAG = "PollCache";
	private static PollCache instance = null;
	
	private Map<Long,Poll> cache;
	
	private PollCache () {
		this.cache = new HashMap<Long,Poll>();
	}
	
	private static PollCache getInstance() {
		if (instance == null)
			instance = new PollCache();
		return instance;
	}
	
	public static Poll get(Long pollId) {
		Poll poll = null;
		if (getInstance().cache.containsKey(pollId)) {
    		poll = getInstance().cache.get(pollId);
    		Log.d(TAG, "Found pollId="+pollId+" in the cache:"+poll);
		}
		return poll;
	}
	
	public static void add(Poll poll) {
		assert(poll != null);
		assert(poll.getId() != null);
		Log.d(TAG, "Adding pollId="+poll.getId()+" to the cache.");
		getInstance().cache.put(poll.getId(), poll);
	}
}
